<template>
  <div class="dashboard-container">
    <div class="welcome-card">
      <h1 class="welcome-text">欢迎, {{ name }}!</h1>
      <p class="welcome-description">您正在管理志愿活动。</p>
    </div>

    <div class="stats-container">
      <div class="stat-card">
        <div class="stat-icon">
          <i class="el-icon-user"></i>
        </div>
        <div class="stat-content">
          <p class="stat-title">志愿者总数</p>
          <p class="stat-value">{{ stats.volunteers }}</p>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon">
          <i class="el-icon-date"></i>
        </div>
        <div class="stat-content">
          <p class="stat-title">即将开展的活动</p>
          <p class="stat-value">{{ stats.upcomingEvents }}</p>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon">
          <i class="el-icon-trophy"></i>
        </div>
        <div class="stat-content">
          <p class="stat-title">已完成的活动</p>
          <p class="stat-value">{{ stats.completedEvents }}</p>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import dashboardApi from "@/api/dashboard"; // 替换为你的后台接口路径

export default {
  data() {
    return {
      name: "管理员", // 默认值，防止数据未返回时的渲染报错
      stats: {
        volunteers: 0, // 志愿者总数
        upcomingEvents: 0, // 即将开展的活动
        completedEvents: 0, // 已完成的活动
      },
    };
  },
  mounted() {
    this.getStats(); // 加载统计数据
  },
  methods: {
    // 获取后台统计数据
    getStats() {
      dashboardApi
        .getStats()
        .then((response) => {
          const data = response.data || {};
          this.stats.volunteers = data.volunteers || 0;
          this.stats.upcomingEvents = data.upcomingEvents || 0;
          this.stats.completedEvents = data.completedEvents || 0;
        })
        .catch((error) => {
          console.error("获取统计数据失败", error);
          this.$message.error("获取统计数据失败，请稍后重试！");
        });
    },
  },
};
</script>


<style lang="scss" scoped>
.dashboard-container {
  margin: 30px;
  display: flex;
  flex-direction: column;
  gap: 30px;
}

.welcome-card {
  background-color: #f0f4f8;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.welcome-text {
  font-size: 36px;
  font-weight: 600;
  color: #333;
}

.welcome-description {
  font-size: 16px;
  color: #666;
}

.stats-container {
  display: flex;
  gap: 20px;
}

.stat-card {
  background-color: #fff;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  width: 30%;
  text-align: center;
}

.stat-icon {
  font-size: 40px;
  color: #4caf50;
}

.stat-title {
  font-size: 18px;
  font-weight: 500;
  color: #333;
}

.stat-value {
  font-size: 24px;
  font-weight: 700;
  color: #4caf50;
}
</style>

